// 写完封装，放到页面的点击事件中
function eatHotdog(){


// 王思聪位置
var wscObj = document.querySelector('.wsc')
document.onmousemove = function(evt){
var e = evt || window.event
var left = e.clientX
// 限制宽度
var maxW = (window.innerWidth || document.documentElement.clientWidth) - wscObj.offsetWidth
if(left<0) left = 0
if(left>=maxW) left = maxW
wscObj.style.left = left + 'px'
}

// 0~9随机数
function random(min,max){
    return Math.floor( Math.random() * (max - min + 1) + min)
}

// 屏幕上方出现随机的食物
t1 = setInterval(function(){
    var imgObj = document.createElement('img')
    imgObj.style.position = 'absolute'
    imgObj.style.top = 0
    var temp = random(0,9)
    // 获取随机的热狗和冠军🏆
    if(temp == 4 || temp == 6){
        imgObj.src = './img/guanjun.jpg'
        imgObj.className = 'champion'

    } else{
        imgObj.src = './img/hotdog.jpg'
        imgObj.className = 'hotdog'
    }
    var maxL =  (window.innerWidth || document.documentElement.clientWidth) - imgObj.offsetWidth
    imgObj.style.left = random(0,maxL) + 'px'
    document.body.appendChild(imgObj)
},400);


/* // 行内样式兼容
function getStyle(tagObj,cssAttr){
    if(window.getComputedStyle){
        return getComputedStyle(tagObj)[cssAttr]
    }else{
        return tagObj.currentStyle[cssAttr]
    }
  } */

// 食物掉落效果
var score = 0
t3 = setInterval(function(){
    var allfoot = document.querySelectorAll('.champion,.hotdog')
// console.log(allfoot);
allfoot.forEach(function(item,i){
    // 获取img的top值，如果是内嵌样式，需要用getComputedStyle(标签对象)[css属性]
    // console.log(parseInt(item.style.top));
    var top = parseInt(item.style.top) + 4
    item.style.top = top + 'px'
    var maxH = window.innerHeight || document.documentElement.clientHeight

    if(parseInt(item.style.top)>maxH){
        document.body.removeChild(item)
    }





//判断王思聪的位置和foot的位置，吃到了就加分
var x = parseInt(item.style.left)
var y = parseInt(item.style.top)
// console.log(y,x);
var wscx = parseInt(getComputedStyle(document.querySelector('.wsc'))['left'])
var wscy = parseInt(getComputedStyle(document.querySelector('.wsc'))['top'])
// console.log(wscx);
// console.log(wscy);
//判断位置
if(wscy<y &&  (x>wscx && x<wscx+80) ){


    // 判断吃的是奖杯还是热狗
    if(item.className === 'hotdog'){
        var scoreObj = document.querySelector('.right span:nth-of-type(2)')
    score++
    document.body.removeChild(item)
    scoreObj.innerText = score

    // 造分数标签，，提示一秒后删除
    var plus = document.createElement('div')
    plus.className = 'score'
    plus.innerText = '+1'
    document.querySelector('.wsc').appendChild(plus)

    // console.log(1);
    // 倒计时定时器删除分数的标签
    setTimeout(function(){
        document.querySelector('.wsc').removeChild(plus)
    },500)


    } else{
        var scoreObj = document.querySelector('.right span:nth-of-type(2)')
        score+=5
        document.body.removeChild(item)
        scoreObj.innerText = score

    // 造分数标签，，提示一秒后删除
        var plus = document.createElement('div')
    plus.className = 'score'
    plus.innerText = '+5'
    document.querySelector('.wsc').appendChild(plus)
    // 倒计时定时器删除分数的标签
    setTimeout(function(){
        // console.log(5);
        document.querySelector('.wsc').removeChild(plus)
    },500)
    }
} 
// var cancel = document.querySelector('.score')

})
},1)




// 倒计时结束清除效果
var s = 10
 t2 = setInterval(function(){
    s--
    if(s==0) {
        clearInterval(t1)
        clearInterval(t2)
        clearInterval(t3)
        // 显示游戏主界面
        document.querySelector('.contain').style.display = 'block'
    
    }
    var timeObj = document.querySelector('.left span:nth-of-type(2)')
    timeObj.innerText = s
    
},1000)

}




//开始游戏与结束游戏界面操作
var startObj = document.querySelector('.contain button:nth-of-type(1)')
var quitObj = document.querySelector('.contain button:nth-of-type(2)')
console.log(startObj);
startObj.onclick =function(){
    document.querySelector('.contain').style.display = 'none'
    eatHotdog()
    // 点击开始游戏重置分数
    num = 0
    document.querySelector('.right span:nth-of-type(2)').innerText = num
}
quitObj.onclick =function(){
    window.close()
}